package com.lz.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.lz.config.OSSAutoConfiguretion;
import com.lz.properties.OSSProperties;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * <pre>
 * @描述：
 * @作者：lizhao
 * @创建时间： 2024/9/19 下午2:08
 * </pre>
 **/
public class OSSUtil {


    private OSSProperties OSSproperties;


    public OSSProperties getOSSproperties() {
        return OSSproperties;
    }

    public void setOSSProperties(OSSProperties ossProperties) {
        this.OSSproperties = ossProperties;
    }

    public void uploadObject(String objectName,
                             InputStream file) {
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OSSproperties.getEndpoint(), OSSproperties.getAccessKeyId(), OSSproperties.getAccessKeySecret());
        try {
            ossClient.putObject(OSSproperties.getBucketName(), objectName, file);
        } catch (Exception oe) {
            System.out.println("出现异常");
        } finally {
            ossClient.shutdown();
        }
    }


    public String uploadFile(MultipartFile file) {

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(OSSproperties.getEndpoint(), OSSproperties.getAccessKeyId(), OSSproperties.getAccessKeySecret());
        try {
            ossClient.putObject(OSSproperties.getBucketName(), file.getOriginalFilename(), file.getInputStream());
        } catch (Exception oe) {
            System.out.println("出现异常");
        } finally {
            ossClient.shutdown();
        }
        return "https://lz111.oss-cn-beijing.aliyuncs.com/" + file.getOriginalFilename();
    }


}